jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.serviceAssemblies.ServiceAssemblies", //the full name of the class to create
        jsx3.gui.LayoutGrid, //  extends
        null, // implements
        function (plugin) {
        
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.prototype.subscribedToPolling;
        	plugin.prototype.screenId;
        	plugin.prototype.selectedServiceAssemblyIds;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
			plugin.showServiceAssemblyDetail = function(serviceAssemblyIdVal) {
        		var paramArray = { serviceAssemblyId:serviceAssemblyIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.serviceAssemblyScreenId, paramArray); 
        	}
        	
			plugin.prototype.setUpComponent = function(screenId) {
				this.subscribedToPolling = false;
				this.screenId = screenId;
				this.selectedServiceAssemblyIds = new jsx3.util.List();
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject, this.environmentSelected, this);
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
			}
			
			plugin.prototype.handleDisplayScreen = function(subject, message, tableObj) {
            	if(message.screenId == tableObj.screenId)
            	{
            		var envId = message.environmentId;
            		if(!envId) 
            		{ envId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
            		if(envId && envId != "" && envId != tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.allEnvironmentsId)
            		{ 
            			var msg = { environmentId:envId };
            			var subject = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject;
            			tableObj.environmentSelected(subject, msg, tableObj);
            		}
            	}
            }
			
			plugin.prototype.environmentSelected = function(subject, message, tableObj) {
            	var envId = message.environmentId;
            	tableObj.callGetServiceAssemblies(envId);
            	
            	if(!tableObj.subscribedToPolling)
            	{
            		tableObj.subscribedToPolling = true; 
            		tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, tableObj, tableObj.updateComponent, tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.environmentScreenId); 
            		tibco.admin.monitormanage.general.RefreshHandler.register(tableObj, tableObj.updateComponent, tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.environmentScreenId); 
            	}
            }
            
            plugin.prototype.updateComponent = function() {
				this.callGetServiceAssemblies();
			}
			
			plugin.prototype.tableSelectionsChanged = function() {
				this.selectedServiceAssemblyIds.clear();
				var enableStart = false;
				var enableStop = false;
				var startButtonState = jsx3.gui.Form.STATEDISABLED;
				var stopButtonState = jsx3.gui.Form.STATEDISABLED;
				var recordIter = this.getDescendantOfName("serviceAssembliesTable").getSelectedNodes().iterator();
                while(recordIter.hasNext()) {
	            	var record = recordIter.next();
                    var status = record.getAttribute("status");
                    if(status == "Stopped" || status == "DEPLOYED")
                    { 
                    	startButtonState = jsx3.gui.Form.STATEENABLED; 
                    	enableStart = true;
                    }
                    else if(status == "Running" || status == "RUNNING")
                    { 
                    	stopButtonState = jsx3.gui.Form.STATEENABLED; 
                    	enableStop = true;
                    }
                    var recordId = record.getAttribute("jsxid");
                    this.selectedServiceAssemblyIds.add(recordId);
                }
                this.getDescendantOfName("svcAsmblisStartBtn_MM_dply").setEnabled(startButtonState, true);
                this.getDescendantOfName("svcAsmblisStopBtn_MM_dply").setEnabled(stopButtonState, true);
			}
			
			plugin.prototype.startServiceAssemblies = function() {
				try {
					var objService;
					var rulesPath = "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/rules/startServiceAssembliesRules.xml";
					var dataPath = "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/data/startServiceAssembliesData.xml";
					if (tibco.admin.Main.getServer().getEnv("MODE")) {
	                    // mode is live
	                    objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, "startServiceAssemblies");
	                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
	                    // this setInboundURL should not be required...only here because of a current AMX framework bug
			    		objService.setInboundURL(dataPath);
	                }
	                else {
	                    objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), "startServiceAssemblies", null, null);
	                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
	                }
                
                    var serviceAssemblyList = new jsx3.util.List(0);
                    var recordIter = this.getDescendantOfName("serviceAssembliesTable").getSelectedNodes().iterator();
                    while(recordIter.hasNext()) {
	                    var record = recordIter.next();
                        var obj = new Object();
                        obj.serviceAssemblyId = record.getAttribute("key");
                        obj.serviceAssemblyName = record.getAttribute("name");
                        //jsx3.log("service assembly key = " + obj.serviceAssemblyId);
                        //jsx3.log("service assembly name = " + obj.serviceAssemblyName);
                        serviceAssemblyList.add(obj, serviceAssemblyList.size());
                    }
                    objService.serviceAssembliesIter = serviceAssemblyList.iterator();
                    
                    objService.serviceAssembliesObj = this;

                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.onstartServiceAssembliesSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this.onstartServiceAssembliesError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this.onstartServiceAssembliesInvalid);
                    objService.doCall();
                }
                catch(ex) {
                	tibco.admin.Main.logException(ex, "Error in tibco.admin.monitormanage.uicomponents.table.serviceAssemblies.ServiceAssemblies.startServiceAssemblies");
                }
			}

		    plugin.prototype.onstartServiceAssembliesSuccess = function(objEvent) {
	      		//var responseXML = objEvent.target.getInboundDocument();
	      		//jsx3.log("Success","The service call was successful.");
	      		objEvent.target.serviceAssembliesObj.callGetServiceAssemblies();
	    	};
	
	    	plugin.prototype.onstartServiceAssembliesError = function(objEvent) {
	      		var myStatus = objEvent.target.getRequest().getStatus();
	      		jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
	    	};
	
	    	plugin.prototype.onstartServiceAssembliesInvalid = function(objEvent) {
	      		jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
	    	};
			
			plugin.prototype.stopServiceAssemblies = function() {
				try {
					var objService;
					var rulesPath = "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/rules/stopServiceAssembliesRules.xml";
					var dataPath = "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/data/stopServiceAssembliesData.xml";
					if (tibco.admin.Main.getServer().getEnv("MODE")) {
	                    // mode is live
	                    objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, "stopServiceAssemblies");
	                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
	                    // this setInboundURL should not be required...only here because of a current AMX framework bug
			    		objService.setInboundURL(dataPath);
	                }
	                else {
	                    objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), "stopServiceAssemblies", null, null);
	                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
	                }
                
                    var serviceAssemblyList = new jsx3.util.List(0);
                    var recordIter = this.getDescendantOfName("serviceAssembliesTable").getSelectedNodes().iterator();
                    while(recordIter.hasNext()) {
	                    var record = recordIter.next();
                        var obj = new Object();
                        obj.serviceAssemblyId = record.getAttribute("key");
                        obj.serviceAssemblyName = record.getAttribute("name");
                        //jsx3.log("service assembly key = " + obj.serviceAssemblyId);
                        //jsx3.log("service assembly name = " + obj.serviceAssemblyName);
                        serviceAssemblyList.add(obj, serviceAssemblyList.size());
                    }
                    objService.serviceAssembliesIter = serviceAssemblyList.iterator();
                    
                    objService.serviceAssembliesObj = this;

                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.onstopServiceAssembliesSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this.onstopServiceAssembliesError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this.onstopServiceAssembliesInvalid);
                    objService.doCall();
                }
                catch(ex) {
                	tibco.admin.Main.logException(ex, "Error in tibco.admin.monitormanage.uicomponents.table.serviceAssemblies.ServiceAssemblies.stopServiceAssemblies");
                }
			}
			
			plugin.prototype.onstopServiceAssembliesSuccess = function(objEvent) {
	      		//var responseXML = objEvent.target.getInboundDocument();
	      		//jsx3.log("Success","The service call was successful.");
	      		objEvent.target.serviceAssembliesObj.callGetServiceAssemblies();
	    	};
	
	    	plugin.prototype.onstopServiceAssembliesError = function(objEvent) {
	      		var myStatus = objEvent.target.getRequest().getStatus();
	      		jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
	    	};
	
	    	plugin.prototype.onstopServiceAssembliesInvalid = function(objEvent) {
	      		jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
	    	};			
			
	    	//call this method to begin the service call (eg.service.callgetServiceAssemblies();)
    		plugin.prototype.callGetServiceAssemblies = function(environmentId) {
				
				if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/rules/serviceAssembliesRules.xml", "getServiceAssemblies");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL("tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/data/serviceAssembliesData.xml");
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/rules/serviceAssembliesRules.xml"), "getServiceAssemblies", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/serviceAssemblies/data/serviceAssembliesData.xml"));
                }
                
                objService.serviceAssembliesObj = this;
                
                if(!environmentId)
				{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
				objService.environmentId = environmentId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("status");
				metricsList.add("deployability");
				metricsList.add("numServiceUnits");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetServiceAssembliesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetServiceAssembliesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetServiceAssembliesInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetServiceAssembliesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			
      			var serviceAssembliesObj = objEvent.target.serviceAssembliesObj;
      			var serviceAssembliesTable = serviceAssembliesObj.getDescendantOfName("serviceAssembliesTable");
      			
      			// reselect the selected rows
      			var selectedIdsIter = serviceAssembliesObj.selectedServiceAssemblyIds.iterator();
      			while(selectedIdsIter.hasNext())
      			{
      				var selectedId = selectedIdsIter.next();
      				serviceAssembliesTable.selectRecord(selectedId);
      			}
      			serviceAssembliesTable.repaintData();

			serviceAssembliesObj.tableSelectionsChanged();
    		};

    		plugin.prototype.ongetServiceAssembliesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetServiceAssembliesInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};
        	
        });